<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }
        form {
            max-width: 400px;
            margin: 0 auto;
            background: white;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        h2 {
            text-align: center;
            color: #333;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input[type="text"],
        input[type="password"] {
            width: 100%;
            padding: 10px;
            margin-bottom: 15px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        input[type="submit"] {
            width: 100%;
            padding: 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        input[type="submit"]:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
    <form id="loginForm">
        <h2>用户登录</h2>
        
        <label for="username">账号：</label>
        <input type="text" id="username" name="username" required>
        
        <label for="password">密码：</label>
        <input type="password" id="password" name="password" required>
        
        <button type="button" id="loginBtn">登录</button>
        <button type="button" id="enrollLink">没有账号，点击注册</button>
    </form>

    <script>
        // 提交数据的通用函数（x-www-form-urlencoded格式）
        async function submitFormData(data) {
            const formData = new URLSearchParams();
            formData.append('username', data.username);
            formData.append('password', data.password);

            try {
                const response = await fetch('/cgi_main', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                    body: formData
                });
                return response.ok;
            } catch (error) {
                console.error('提交出错:', error);
                return false;
            }
        }

        // 登录功能
        document.getElementById('loginBtn').addEventListener('click', async function() {
            const form = document.getElementById('loginForm');
            const data = {
                username: form.username.value,
                password: form.password.value
            };

            const success = await submitFormData(data);
            if (success) {
                // alert('登录成功！');
                // 实际项目中这里跳转到用户主页
                window.location.href = '/functions/linux_web_inter.html';
            } else {
                alert('登录失败，请检查账号密码');
            }
        });

        // 注册跳转功能
        document.getElementById('enrollLink').addEventListener('click', async function(e) {
            e.preventDefault();

            const form = document.getElementById('loginForm');
            const data = {
                username: form.username.value,
                password: form.password.value
            };

            const success = await submitFormData(data);
            if (success) {
                window.location.href = '/enroll.html';
            } else {
                alert('提交失败，请重试');
            }
        });
    </script>
</body>

<!-- <body>
    <form id="loginForm" action="/enroll.html" method="post">
        <h2>用户登录</h2>
        
        <label for="username">账号：</label>
        <input type="text" id="username" name="username" required>
        
        <label for="password">密码：</label>
        <input type="password" id="password" name="password" required>
        
        <input type="submit" formaction="/cgi_main" value="登录">

        <a href="#" id="enrollLink"><button type="button">没有账号，点击注册</button></a>
    </form>

    <script>
        document.getElementById('enrollLink').addEventListener('click', async function(e) {
            e.preventDefault(); // 阻止默认行为

            const form = document.getElementById('loginForm');
            const formData = new FormData(form);

            try {
                // 使用 Fetch API 提交数据到 CGI 后端
                const response = await fetch('/cgi_main', {
                    method: 'POST',
                    body: formData
                });

                if (!response.ok) {
                    throw new Error('提交失败');
                }

                // 提交成功后跳转到注册页面
                window.location.href = '/enroll.html';
            } catch (error) {
                console.error('提交出错:', error);
                alert('提交失败，请检查网络后重试');
            }
        });
    </script>
</body>
</html> -->

<!-- <body>
    <form id="loginForm" action="/enroll.html" method="post">
        <h2>用户登录</h2>
        
        <label for="username">账号：</label>
        <input type="text" id="username" name="username" required>
        
        <label for="password">密码：</label>
        <input type="password" id="password" name="password" required>
        
        <input type="submit" formaction="/cgi_main" value="登录">

        <a href="#" id="enrollLink"><button type="submit">没有账号，点击注册</button></a>
    </form>

    <script>
        document.getElementById('enrollLink').addEventListener('click', function(e) {
            e.preventDefault(); // 阻止默认的链接跳转行为
            
            // 创建一个隐藏的iframe来提交表单
            var iframe = document.createElement('iframe');
            iframe.name = 'hiddenFrame';
            iframe.style.display = 'none';
            document.body.appendChild(iframe);
            
            // 临时修改表单的target和action
            var form = document.getElementById('loginForm');
            var originalAction = form.action;
            var originalTarget = form.target;
            
            form.action = '/cgi_main'; // 提交到CGI后端
            form.target = 'hiddenFrame'; // 在隐藏iframe中提交
            
            // 提交表单
            form.submit();
            
            // 恢复原始表单设置
            form.action = originalAction;
            form.target = originalTarget;
            
            // 跳转到enroll.html
            window.location.href = '/enroll.html';
        });
    </script>
</body>
</html> -->

<!-- <body>
    <form id="loginForm" action="/enroll.html" method="post">
        <h2>用户登录</h2>
        
        <label for="username">账号：</label>
        <input type="text" id="username" name="username" required>
        
        <label for="password">密码：</label>
        <input type="password" id="password" name="password" required>
        
        <input type="submit" formaction="/cgi_main" value="登录">

        <a href="#" id="enrollLink">注册</a>
    </form>

    <script>
        document.getElementById('enrollLink').addEventListener('click', function(e) {
            e.preventDefault(); // 阻止默认的链接跳转行为
            
            // 创建一个隐藏的iframe来提交表单
            var iframe = document.createElement('iframe');
            iframe.name = 'hiddenFrame';
            iframe.style.display = 'none';
            document.body.appendChild(iframe);
            
            // 临时修改表单的target和action
            var form = document.getElementById('loginForm');
            var originalAction = form.action;
            var originalTarget = form.target;
            
            form.action = '/cgi_main'; // 提交到CGI后端
            form.target = 'hiddenFrame'; // 在隐藏iframe中提交
            
            // 提交表单
            form.submit();
            
            // 恢复原始表单设置
            form.action = originalAction;
            form.target = originalTarget;
            
            // 跳转到enroll.html
            window.location.href = '/enroll.html';
        });
    </script>
</body>
</html> -->